﻿using BatchPrint.Models;
using BatchPrint.Windows;
using System.IO;
using System.Windows;

namespace BatchPrint;

/// <summary>
/// ProgressWindow.xaml 的交互逻辑
/// </summary>
public partial class ProgressWindow : Window
{
    private List<PrintModel>? _files;
    private readonly ProgressWindowViewModel _viewModel;

    public ProgressWindow(ProgressWindowViewModel viewModel)
    {
        InitializeComponent();
        DataContext = viewModel;
        _viewModel = viewModel;
        _viewModel.OnPrintFinish += _viewModel_OnPrintFinish;
    }

    private void _viewModel_OnPrintFinish()
    {
        App.Current.Dispatcher.Invoke(() =>
        {
            this.DialogResult = true;
            this.Close();
        });
    }

    public void StartPrint(List<PrintModel> files, string printerName)
    {
        this._files = files;
        if (this.DataContext is ProgressWindowViewModel viewModel)
        {
            foreach (var item in files)
                viewModel.Files.Add(item);

            viewModel.PrintName = printerName;
            this.Owner = App.Current.MainWindow;
            this.Width = App.Current.MainWindow.ActualWidth;
            this.Height = App.Current.MainWindow.ActualHeight;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.ShowDialog();
        }
    }
}
